#!/bin/sh

#logger ACTION=$ACTION DEVPATH=$DEVPATH SUBSYSTEM=$SUBSYSTEM DEVTYPE=$DEVTYPE PRODUCT=$PRODUCT MODALIAS=$MODALIAS

PLATFORM='sun8iw7p1|sun50iw2p1'
if ! grep -E $PLATFORM /sys/class/sunxi_info/sys_info -q; then
        logger "only support $PLATFORM. exiting..."
        exit 0
fi

CONFIG=/etc/config/network
MODULE=e8372h
TARGET=eth_$MODULE
if [ "$DEVTYPE" = usb_device -a "$PRODUCT" = "12d1/14db/102" ]; then
        if [ "$ACTION" = bind ]; then
                logger "Huawei WiFi 2 mini(E8372h) insert"
                NEW=`find /sys/$DEVPATH -name eth*`
                NEW=`basename $NEW`
                logger "rename $NEW to $TARGET"
                ip link set $NEW name $TARGET
                
                logger "$CONFIG update"
                mkdir -p /root/.friendlyelec/$MODULE
                cp $CONFIG /root/.friendlyelec/$MODULE/network
                if grep eth0 $CONFIG -q; then
                        sed -i "s/eth0/$TARGET/g" $CONFIG
                else
                        cat << EOF >> $CONFIG
config interface 'wan'
        option proto 'dhcp'               
        option ifname 'eth_$MODULE'              
        option dns '8.8.8.8'
EOF
                fi
                /etc/init.d/network reload
        elif [ "$ACTION" = unbind ]; then
                logger "Huawei WiFi 2 mini(E8372h) eject"
                logger "/etc/config/network restore"
                cp /root/.friendlyelec/$MODULE/network $CONFIG
                /etc/init.d/network reload
        fi
fi
